package cn.big.bigd.biocode.service;

import cn.big.bigd.biocode.dao.VisitTrackDAO;
import cn.big.bigd.biocode.model.VisitTrack;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.Date;

/**
 * Created by wangfan on 2017/12/4.
 */
@Service
public class VisitTrackService {


    @PersistenceContext
    private EntityManager entityManager;

    @Autowired
    VisitTrackDAO visitTrackDAO;


    public VisitTrack   increment(String countrycode,String taregetType,Integer targetId){
       VisitTrack visitTrack= visitTrackDAO.findByDateAndCountryAndTargetTypeAndTargetId(new Date(),countrycode,taregetType,targetId);
       if(null==visitTrack){
           visitTrack =new VisitTrack() ;
           visitTrack.setCountry(countrycode);
           visitTrack.setTargetType(taregetType);
           visitTrack.setTargetId(targetId);
           visitTrack.setDate(new Date());
           visitTrack.setCount(1);
       }else{
           if(null==visitTrack.getCount()){

               visitTrack.setCount(1);

           }else {
               visitTrack.setCount(visitTrack.getCount()+1);
           }
       }

       return visitTrackDAO.save(visitTrack);
    }




}
